package com.ss.android.article.base.feature.f;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.newmedia.BaseAppData;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class f {
    final class a extends BroadcastReceiver {
        private final f a;

        a(f arg1, byte arg2) {
            this(arg1);
        }

        private a(f arg1) {
            this.a = arg1;

        }

        public final void onReceive(Context arg3, Intent arg4) {
            if (("android.net.conn.CONNECTIVITY_CHANGE".equals(arg4.getAction())) && (this.a.g)) {
                new c().start();
            }
            this.a.g = true;
        }
    }

    public final class b extends AbsApiThread {
        private JSONObject a;
        private String b;
        private int c;
        private boolean d;

        public b(JSONObject jsonObject, String arg3, int arg4, boolean arg5) {
            this.d = false;
            this.a = jsonObject;
            this.b = arg3;
            this.c = arg4;
            this.d = arg5;
        }

        private boolean a(String arg2) {
            return this.a != null
                    && (!android.support.design.a.isTextEmpty(arg2))
                    && this.a.has(arg2);
        }

        public final void run() {
            URLConnection v1_3;
            String v6;
            URLConnection v0_4 = null;
            int v5 = -1;
            String v2_1 = "";
            String v3_2;
            String[] v7;
            int v0;
            int v3 = 3;
            int v4 = 2;
            int v2 = 0;
            if ((BaseAppData.inst().mVideoStatisticsFlag & 1) > 0) {
                if (this.c <= 0 && !this.a("lt")) {
                    v0 = 0;
                } else {
                    v0 = 1;
                }
            } else {
                v0 = 0;
            }

            String[] v3_1 = new String[v3];
            if (v0 == 0) {
            } else if (!TextUtils.isEmpty(com.ss.android.article.base.feature.f.f.b)) {
            } else {
                Logger.d("VideoStatistics", "fetchDNSIP");
                com.ss.android.article.base.feature.f.f.a(v3_1);
                com.ss.android.article.base.feature.f.f.a = v3_1[0];
                com.ss.android.article.base.feature.f.f.b = v3_1[1];
                try {
                    if (this.a == null) {
                    } else if (android.support.design.a.isTextEmpty(v3_1[2])) {
                    } else {
                        com.ss.android.article.base.feature.f.f.a(this.a, "error", "DNS_ERR", v3_1[2]);
                    }
                } catch (Exception v0_1) {
                    Logger.e("VideoStatistics", "error when fetch DNSIP : " + v0_1.toString());
                }
            }

            try {
                if (this.a != null && !android.support.design.a.isTextEmpty(com.ss.android.article.base.feature.f.f.a)) {
                    this.a.put("ip", com.ss.android.article.base.feature.f.f.a);
                }

                if (this.a == null) {
                } else if (android.support.design.a.isTextEmpty(com.ss.android.article.base.feature.f.f.b)) {
                } else {
                    this.a.put("dns", com.ss.android.article.base.feature.f.f.b);
                }
            } catch (Exception v0_1) {
            }

            try {
                if ((BaseAppData.inst().mVideoStatisticsFlag & 2) > 0 && (this.c > 0 || (this.a("lt")))) {
                    v2 = 1;
                }

                if (v2 == 0) {
                } else {
                    Logger.d("VideoStatistics", "fetch ServerIP");
                    v7 = new String[3];
                    v3_2 = this.b;
                    if (!TTUtils.isValidUrl(v3_2)) {
                        try {
                            v0_4 = new URL(v3_2).openConnection();
                            ((HttpURLConnection) v0_4).setInstanceFollowRedirects(false);
                            ((HttpURLConnection) v0_4).setConnectTimeout(5000);
                            if ((BaseAppData.inst().mVideoStatisticsFlag & 4) <= 0 || !v3_2.contains("v2.pstatp.com")) {
                                Logger.d("VideoStatistics", "Get SI by GET");
                                ((HttpURLConnection) v0_4).setRequestMethod("GET");
                                ((HttpURLConnection) v0_4).connect();
                                v6 = ((HttpURLConnection) v0_4).getHeaderField("location");
                                Matcher v8 = com.ss.android.article.base.feature.f.f.e(v6);
                                if (v8 != null && (v8.find())) {
                                    v2_1 = v8.group();
                                }
                                v3_2 = v6;
                            } else {
                                Logger.d("VideoStatistics", "Get SI by HEAD");
                                ((HttpURLConnection) v0_4).setRequestMethod("HEAD");
                                ((HttpURLConnection) v0_4).connect();
                                v6 = ((HttpURLConnection) v0_4).getHeaderField("Fw-Via");
                                if (!android.support.design.a.isTextEmpty(v6)) {
                                    Matcher v6_1 = com.ss.android.article.base.feature.f.f.e(v6);
                                    if (v6_1 != null && (v6_1.find())) {
                                        v2_1 = v6_1.group();
                                    }
                                }
                            }
                            ((HttpURLConnection) v0_4).disconnect();
                            v0 = v5;
                        } catch (ProtocolException v0_3) {
                            v0_3.printStackTrace();
                            v0 = AppLog.w();
                        } catch (MalformedURLException v0_2) {
                            v0_2.printStackTrace();
                            v0 = AppLog.w();
                        } catch (Exception v0_1) {
                            v0_1.printStackTrace();
                            v0 = AppLog.w();
                        }
                        v7[0] = v2_1;
                        v7[1] = v3_2;
                        if (v0 > 0) {
                            v7[2] = "ERR:" + v0;
                        }
                    }

                    if (this.a != null && !android.support.design.a.isTextEmpty(v7[0])) {
                        this.a.put("si", v7[0]);
                    }

                    if (this.a != null && !android.support.design.a.isTextEmpty(v7[1])) {
                        this.a.put("vu", v7[1]);
                    }

                    if (this.a == null) {
                    } else if (android.support.design.a.isTextEmpty(v7[2])) {
                    } else {
                        com.ss.android.article.base.feature.f.f.a(this.a, "error", "SIP_ERR", v7[2]);
                    }
                }
            } catch (Exception v0_1) {
            }

            if (!this.d) {

            } else if (!BaseAppData.inst().videoDiagnosis()) {

            } else {
                v2 = 1000;
                try {
                    Uri v0_5 = Uri.parse(this.b);
                    if (v0_5 == null) {
                    } else {
                        v0_5.getHost();
                    }
                } catch (Exception v0_1) {
                    v2 = 1;
                }
                if (v2 == 1) {
                    v0 = v2;
                } else {
                    try {
                        String v1 = null;
                        v0_4 = new URL(this.b).openConnection();
                        v1 = "HEAD";
                        label_172:
                        ((HttpURLConnection) v0_4).setRequestMethod(v1);
                        v0_4.connect();
                        int v1_4;
                        if (((HttpURLConnection) v0_4).getResponseCode() == 200) {
                            v1_4 = v2;
                        } else {
                            v1_4 = v4;
                        }
                        if (v0_4 != null) {
                            ((HttpURLConnection) v0_4).disconnect();
                        }
                        v0 = v1_4;
                    } catch (Exception e) {
                        v0 = v4;
                    } finally {
                        if (v0_4 != null) {
                            ((HttpURLConnection) v0_4).disconnect();
                        }
                    }
                }
                try {
                    this.a.put("errt", v0);
                } catch (JSONException v0_8) {
                }
            }
            com.ss.android.article.base.feature.f.f.a(this.a);
            if (this.a != null) {
                AppLog.a("video_playq", this.a);
            }
        }
    }

    public final class c extends AbsApiThread {
        public c() {

        }

        public final void run() {
            Logger.d("VideoStatistics", "parse new DNSIP");
            String[] v0 = new String[3];
            com.ss.android.article.base.feature.f.f.a(v0);
            com.ss.android.article.base.feature.f.f.a = v0[0];
            com.ss.android.article.base.feature.f.f.b = v0[1];
        }
    }

    static String a;
    static String b;
    public JSONObject c;
    public int d;
    public boolean e;
    public String f;
    public boolean g;
    private Context h;
    private BaseAppData i;
    private a j;
    private String k;
    private boolean l;

    public f(Context arg4, String arg5, String arg6, String arg7) {
        boolean v0 = false;

        this.d = 0;
        this.e = false;
        this.f = "";
        this.l = false;
        this.g = false;
        this.i = BaseAppData.inst();
        this.h = arg4.getApplicationContext();
        this.a(arg5, arg6, arg7);
        if (Build.VERSION.SDK_INT >= 17) {
            v0 = true;
        }

        this.e = v0;
    }

    public final void a(String arg4, String arg5, String arg6, String arg7, String arg8) {
        this.a();
        if (!android.support.design.a.isTextEmpty(arg4)) {
            com.ss.android.article.base.feature.f.f.a(this.c, "api_error", "url", arg4);
        }

        if (!android.support.design.a.isTextEmpty(arg5)) {
            com.ss.android.article.base.feature.f.f.a(this.c, "api_error", "error1", arg5);
        }

        if (!android.support.design.a.isTextEmpty(arg6)) {
            com.ss.android.article.base.feature.f.f.a(this.c, "api_error", "error2", arg6);
        }

        if (!android.support.design.a.isTextEmpty(arg7)) {
            com.ss.android.article.base.feature.f.f.a(this.c, "api_error", "data_error1", arg7);
        }

        if (!android.support.design.a.isTextEmpty(arg8)) {
            com.ss.android.article.base.feature.f.f.a(this.c, "api_error", "data_error2", arg8);
        }
    }

    public final void a(String arg3, long arg4) {
        if (!android.support.design.a.isTextEmpty(arg3) && (arg4 >= 0 && this.c != null)) {
            try {
                this.c.put(arg3, arg4);
            } catch (JSONException v0) {
                v0.printStackTrace();
            }
        }
    }

    public final void a(String arg2) {
        this.b("pt");
        if (!TextUtils.isEmpty(((CharSequence) arg2))) {
            this.a("wid", arg2);
            this.f = arg2;
        }
    }

    public final void a(String arg3, Object arg4) {
        try {
            com.ss.android.article.base.feature.f.f.a(this.c, "error", arg3, arg4);
        } catch (Exception v0) {
        }
    }

    public final void a() {
        if (!this.c("vt")) {
            this.b("lt");
        } else {
            this.b("et");
        }
    }

    public final void a(String arg4, String arg5, String arg6) {
        this.c = new JSONObject();
        this.a("sv", arg5);
        this.a("pc", arg6);
        this.a("v", arg4);
        this.a("n", NetworkUtils.getNetworkAccessType(this.h));
        this.a("m", NetworkUtils.getNetworkOperatorCode(this.h));
        Logger.d("VideoStatistics", "registerConnectivityReceiver");
        int v0 = (this.i.mVideoStatisticsFlag & 1) > 0 ? 1 : 0;
        if (v0 != 0) {
            if (this.j == null) {
                this.j = new a(this, (byte) 0);
            }

            IntentFilter v0_1 = new IntentFilter();
            v0_1.addAction("android.net.conn.CONNECTIVITY_CHANGE");
            this.h.registerReceiver(this.j, v0_1);
        }
    }

    public static void a(JSONObject arg2, String arg3, Object arg4) {
        JSONObject v0_1;
        if (arg2 != null && !android.support.design.a.isTextEmpty(arg3)) {
            try {
                if (arg2.has("ex")) {
                    v0_1 = arg2.getJSONObject("ex");
                } else {
                    v0_1 = null;
                }

                if (v0_1 == null) {
                    v0_1 = new JSONObject();
                    arg2.put("ex", v0_1);
                }

                v0_1.put(arg3, arg4);
            } catch (JSONException v0) {
                label_17:
                v0.printStackTrace();
            }
        }
    }

    public final void a(boolean arg2) {
        if (!arg2) {
            if (this.c("vt")) {
                this.b("et");
            } else if (!this.c("lt")) {
                this.b("lt");
            }
        }

        this.e();
    }

    private void a(String arg2, String arg3) {
        if (!android.support.design.a.isTextEmpty(arg2) && (arg3 != null && this.c != null)) {
            try {
                this.c.put(arg2, arg3);
            } catch (JSONException v0) {
                v0.printStackTrace();
            }
        }
    }

    static void a(JSONObject arg5) {
        StringBuffer v0_2;
        if (arg5 != null) {
            Iterator v2 = arg5.keys();
            StringBuffer v1 = new StringBuffer();
            try {
                while (v2.hasNext()) {
                    Object v0_1 = v2.next();
                    if (v0_1 == null) {
                        continue;
                    }

                    v1.append(((String) v0_1)).append(':').append(String.valueOf(arg5.get(((String) v0_1)))).append("\n");
                }
                v0_2 = v1;
            } catch (Exception v0) {
                v0_2 = null;
            }
            if (v0_2 == null) {
                return;
            }

            Logger.d("VideoStatistics", "video statistics: \n" + v0_2);
        }
    }

    public static void a(JSONObject arg3, String arg4, String arg5, Object arg6) {
        JSONObject v1;
        JSONObject v0 = null;
        if (arg3 != null && !android.support.design.a.isTextEmpty(arg4) && !android.support.design.a.isTextEmpty(arg5)) {
            try {
                if (arg3.has("ex")) {
                    v1 = arg3.getJSONObject("ex");
                } else {
                    v1 = v0;
                }

                if (v1 == null) {
                    v1 = new JSONObject();
                    arg3.put("ex", v1);
                }

                if (v1.has(arg4)) {
                    v0 = v1.getJSONObject(arg4);
                }

                if (v0 == null) {
                    v0 = new JSONObject();
                    v1.put(arg4, v0);
                }

                v0.put(arg5, arg6);
            } catch (JSONException v0_1) {
                label_27:
                v0_1.printStackTrace();
            }
        }
    }

    public static void a(String[] arg5) {
        String v0_2;
        int v0 = 204800;
        try {
            v0_2 = NetworkUtils.doGet(v0, "http://trace-ldns.ksyun.com/getlocaldns", false);
            if (TextUtils.isEmpty(((CharSequence) v0_2))) {
            } else {
                JSONObject v1 = new JSONObject(v0_2);
                arg5[0] = v1.optString("ClientIP");
                arg5[1] = v1.optString("LocalDnsIP");
            }
        } catch (Exception v0_1) {
        }

        if ((TextUtils.isEmpty(arg5[0])) || (TextUtils.isEmpty(arg5[1]))) {
            v0 = 204800;
            try {
                v0_2 = NetworkUtils.doGet(v0, "http://tools.fastweb.com.cn/Index/Diag", false);
                if (android.support.design.a.isTextEmpty(v0_2)) {
                    return;
                }

                int v1_1 = v0_2.indexOf("\"", v0_2.indexOf("<iframe")) + 1;
                v0_2 = v0_2.substring(v1_1, v0_2.indexOf("\"", v1_1));
                if (!TTUtils.isValidUrl(v0_2)) {
                    return;
                }

                v0_2 = NetworkUtils.doGet(204800, v0_2, false);
                if (android.support.design.a.isTextEmpty(v0_2)) {
                    return;
                }

                v1_1 = v0_2.indexOf("\"", v0_2.indexOf("<iframe")) + 1;
                v0_2 = v0_2.substring(v1_1, v0_2.indexOf("\"", v1_1));
                if (!TTUtils.isValidUrl(v0_2)) {
                    return;
                }

                v0_2 = NetworkUtils.doGet(204800, v0_2, false);
                if (android.support.design.a.isTextEmpty(v0_2)) {
                    return;
                }

                Matcher v0_3 = com.ss.android.article.base.feature.f.f.e(v0_2);
                if (v0_3 == null) {
                    return;
                }

                if (v0_3.find()) {
                    arg5[0] = v0_3.group();
                }

                if (!v0_3.find()) {
                    return;
                }

                arg5[1] = v0_3.group();
            } catch (Exception v0_1) {
                v0_1.printStackTrace();
                Logger.e("VideoStatistics", "error fetch DNSIP : " + v0_1.toString());
                v0 = AppLog.w();
                if (v0 <= 0) {
                    return;
                }

                arg5[2] = "ERR:" + v0;
            }
        }
    }

    public final void b(String arg3) {
        this.a(arg3, System.currentTimeMillis());
    }

    public final void b() {
        this.l = true;
        com.ss.android.article.base.feature.f.f.a(this.c, "network_error", "timeout");
    }

    public final boolean c(String arg2) {
        boolean v0 = this.c == null || (android.support.design.a.isTextEmpty(arg2)) || !this.c.has(arg2) ? false : true;
        return v0;
    }

    public final JSONObject c() {
        if (this.c("vt")) {
            this.b("et");
        } else {
            this.a();
        }

        if ((this.c("vt")) && (this.c("et")) && this.d > 0) {
            this.a("bc", ((long) this.d));
        }

        JSONObject v0 = this.c;
        this.e();
        return v0;
    }

    public final void d() {
        if (this.c("lt")) {
            String v0 = "lt";
            if (android.support.design.a.isTextEmpty(v0)) {
            } else if (this.c != null) {
                try {
                    this.c.remove(v0);
                } catch (Exception v0_1) {
                    v0_1.printStackTrace();
                }
            }
        }
        if (!this.c("vt")) {
            this.b("vt");
        }
    }

    public final void d(String arg2) {
        if ((TTUtils.isValidUrl(arg2)) && this.c != null) {
            this.l = false;
            this.k = arg2;
            this.a("vu", arg2);
        }
    }

    public final void e() {
        a v5 = null;
        Logger.d("VideoStatistics", "unregisterConnectivityReceiver");
        if (this.j != null) {
            this.h.unregisterReceiver(this.j);
            this.j = v5;
        }

        if (this.c != null) {
            new b(this.c, this.k, this.d, this.l).start();
            this.l = false;
            this.c = ((JSONObject) null);
            this.d = 0;
            this.k = ((String) null);
        }
    }

    static Matcher e(String arg2) {
        Matcher v0 = null;
        if (!android.support.design.a.isTextEmpty(arg2)) {
            Pattern v1 = Pattern.compile("(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)");
            if (v1 != null) {
                v0 = v1.matcher(((CharSequence) arg2));
            }
        }
        return v0;
    }
}

